package com.lulu.lulurpc.fault.tolerant;

import com.lulu.lulurpc.model.RpcResponse;
import com.lulu.lulurpc.proxy.MockServiceProxy;
import com.lulu.lulurpc.proxy.ServiceProxyFactory;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;
import java.util.Map;

/**
 * @author wlc
 * @version 1.0
 */
@Slf4j
public class FailBackTolerantStrategy implements TolerantStrategy{
    @Override
    public RpcResponse doTolerant(Map<String, Object> context, Exception e) {
        // todo 可自行扩展，获取降级的服务并调用
        MockServiceProxy mockServiceProxy = new MockServiceProxy();
        Object result = null;
        try {
            result = mockServiceProxy.invoke(null, (Method) context.get("method"), null);
        } catch (Throwable throwable) {
            log.info("fail back to mock 异常！",throwable);
        }
        RpcResponse rpcResponse = new RpcResponse();
        rpcResponse.setData(result);
        return rpcResponse;
    }
}
